package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.service.ResponseService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ResponseServiceImpl implements ResponseService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;



    @Override
    public Map<String, Object> getReportData() throws Exception {

            Map<String,Object> dataMap = new HashMap<>();
            //本日 日期
        String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
                dataMap.put("reportDate", reportDate);
        //当天新增会员
        int byday = this.memberDao.findByday(reportDate);
        dataMap.put("todayNewMember", byday);
          //总会员数
        int totalMember = this.memberDao.findAllByCount();

        dataMap.put("totalMember", totalMember);

        //本周一的日期  本周新增会员
        String weekday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //本周末的日期
        String dayWeek = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
        int thisWeekNewMember = this.memberDao.countByWeek(weekday, dayWeek);

        dataMap.put("thisWeekNewMember",thisWeekNewMember);

        //本月新增会员
        //本月一号的日期
        String months = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
      int thisMonthNewMember = this.memberDao.findByMonth(months,reportDate);
        dataMap.put("thisMonthNewMember",thisMonthNewMember);

        //今日预约数

       int todayOrderNumber = this.orderDao.findByOdr(reportDate);
        dataMap.put("todayOrderNumber",todayOrderNumber);

       //今日到诊数
        int todayVisitsNumber = this.orderDao.findByArrival(reportDate);
        dataMap.put("todayVisitsNumber",todayVisitsNumber);

        //本周预约数
        int thisWeekOrderNumber = this.orderDao.findByWeek(dayWeek,weekday);
            dataMap.put("thisWeekOrderNumber",thisWeekOrderNumber);
         //本周到诊数
        int thisWeekVisitsNumber =  this.orderDao.findByArrivalWeek(weekday, dayWeek);

        dataMap.put("thisWeekVisitsNumber",thisWeekVisitsNumber);


        //设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获得实体类
        Calendar ca = Calendar.getInstance();
        //设置最后一天
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //最后一天格式化
        String lastDay = format.format(ca.getTime());


        //本月预约数
       int thisMonthOrderNumber = this.orderDao.findByMonths(lastDay,months);
        dataMap.put("thisMonthOrderNumber",thisMonthOrderNumber);

        //本月到诊数
        int thisMonthVisitsNumber = this.orderDao.findByArrivalWeek(lastDay,months);
        dataMap.put("thisMonthVisitsNumber" ,thisMonthVisitsNumber);

        //热门套餐
        List<Map<String, Object>> setmealNames = this.orderDao.hotSetmealMap();

//        List<String> setmealNames = new ArrayList<>();
//        for (Map<String, Object> map : setmealCount) {
//
//            String name = (String) map.get("name");
//            setmealNames.add(name);
//        }
        dataMap.put("hotSetmeal",setmealNames);


        return dataMap;
    }


}
